package main

import (
    "bytes"
    "fmt"
    "net"
    "strings"
)

func checkIPInPool(pool string, ipStr string) bool {
    ipScope := strings.Split(pool, "-")
    startIp := net.ParseIP(ipScope[0])
    endIp := net.ParseIP(ipScope[1])
    ip := net.ParseIP(ipStr)

    if bytes.Compare(ip, startIp) >= 0 && bytes.Compare(ip, endIp) <= 0 {
        fmt.Printf("%v is between %v and %v\n", ip, startIp, endIp)
        return true
    }

    return false
}

func canIpPoolUpdate(ip string, oldPool string, newPool string) bool {
    fmt.Println(ip, oldPool, newPool)
    networkPool := strings.Split(oldPool, "-")
    networkPoolStart := net.ParseIP(networkPool[0])
    networkPoolEnd := net.ParseIP(networkPool[1])

    reqPool := strings.Split(newPool, "-")
    reqPoolStart := net.ParseIP(reqPool[0])
    reqPoolend := net.ParseIP(reqPool[1])

    curIp := net.ParseIP(ip)
    fmt.Println("start:", bytes.Compare(reqPoolStart, networkPoolStart))
    fmt.Println("end", bytes.Compare(reqPoolend, networkPoolEnd))
    fmt.Println(curIp)
    if  bytes.Compare(reqPoolStart, networkPoolStart) > 0 ||
        bytes.Compare(reqPoolend, networkPoolEnd) < 0 {
        fmt.Println(bytes.Compare(reqPoolStart, networkPoolStart), bytes.Compare(reqPoolend, networkPoolEnd))
        if checkIPInPool(oldPool, ip) == true {
            fmt.Println("curIP in network pool, cannot update network")
            return false
        }
    }

    return true
}

func main() {
    ipCidr := "1.1.1.1/22"
    _, inet, _ := net.ParseCIDR(ipCidr)
    netStr:= net.IP(inet.Mask).String()

    fmt.Println(netStr)
    fmt.Println(checkIPInPool("192.168.1.1-192.168.1.254", "192.168.1.3"))

    fmt.Println(canIpPoolUpdate("192.168.1.2",
        "192.168.1.1-192.168.1.253",
        "192.168.1.1-192.168.1.254"))
}
